package 华北电力大学;

import bean.ExBean;
import com.alibaba.excel.EasyExcel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Reptile {
    static List<ExBean> exBeans = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        Reptile.run();
    }

    public static void run() throws Exception {
        ExBean exBean;
        List<ExBean> exBeans = new ArrayList<>();

        String a[] = {"dlxtyjs", "xddzkxjsyjs", "dcycddgyjs", "rxdljsyjs", "dldzwlyjs", "gdyyjyjsyjs", "dnzhyjnjsyjs", "dlscyjs", "spdxtyjs", "txjsyjs", "dwyjs", "sfyjs", "dzxxjsyjs", "xnydwyjs", "dgdzjxzx"};
        String b[] = {"电力系统研究所", "现代化电子科学技术研究所", "电磁与超导电工研究所", "柔性电力技术研究所", "电力电子网络研究所", "高电压与绝缘技术研究所", "电能转换与节能技术研究所", "电力市场研究所", "输配电系统研究所", "通信技术研究所", "电网研究所", "四方研究所", "电子信息技术研究所", "新能源电网研究所", "电工电子教学中心"};
        int count = 1;
        for (int i = 0; i < a.length; i++) {

            // 因为要爬取分页信息，就通过循环重新加载网页来来实现，一般网页的分页URL都是有规律的，一般就是在后面的数字加1得到，所以利用这个规律来实现分页爬取数据的效果
            // 网页地址
            String url = "https://electric.ncepu.edu.cn/szdw/xyjj6/" + a[i] + "/index.htm";
            Document document = Jsoup.connect(url).post();
//            System.out.println(document);
//            System.out.println(b[i] + "+++++===============+++++");
            Elements elementsByTag = document.getElementsByClass("subLightYearsList").get(0).getElementsByTag("li");
            int size = elementsByTag.size();
            for (int j = 0; j < size; j++) {
                try {
                    exBean = new ExBean();
                    String Url = "https://electric.ncepu.edu.cn/szdw/xyjj6/" + a[i] + "/" + elementsByTag.get(j).getElementsByTag("a").attr("href");
                    Document post = Jsoup.connect(Url).post();
                    Elements elementsByTag1 = post.getElementsByClass("article").get(0).getElementsByTag("p");
                    for (Element element : elementsByTag1) {

                        if (element.text().contains("姓名")) {
                            String name = Reptile.Delete(element.text(), "姓名：").trim();
                            exBean.setName(name.trim());
                        } else if (element.text().contains("职称")) {
                            if (element.text().contains("教授")) {
                                if (element.text().length() == 0) {
                                    break;
                                } else {
                                    String name = Reptile.Delete(element.text(), "职称：");
                                    exBean.setZhiCheng(name);
                                }
                            } else {
                                if (element.text().length() == 0) {
                                    break;
                                }
                            }
                        } else if (element.text().contains("所在院系")) {
                            String name = Reptile.Delete(element.text(), "所在院系：");
                            exBean.setSzYuanX(name);
                        } else if (element.text().contains("研究方向")) {

                        } else if (element.text().contains("Ø")) {
                            String name = Reptile.Delete(element.text(), "Ø");
//                            System.out.println(name);
                            exBean.setYjFx(name);
                        } else if (element.text().contains("办公地址")) {
                            String name = Reptile.Delete(element.text(), "办公地址：");
                            exBean.setSite(name);
                        } else if (element.text().contains("电子邮箱")) {
                            String name = Reptile.Delete(element.text(), "电子邮箱：");
                            exBean.setE_emil(name);
                        } else if (element.text().contains("办公电话")) {
                            String name = Reptile.Delete(element.text(), "办公电话：");
                            exBean.setPhone(name);
                        }
                    }
//                    if (exBean.getName() == null) {
//                        break;
//                    } else {
                    exBeans.add(exBean);
//                    }
                    try {
                        for (ExBean bean : exBeans) {
                            if (bean.getName() == null) {
                                exBeans.remove(bean);
                            }
                        }
                    } catch (Exception e) {
//                        throw new RuntimeException(e);
                        System.out.println(j);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        EasyExcel.write("src/user.xlsx", ExBean.class).sheet("user").doWrite(exBeans);
    }

    //指定字符串删除类
    public static String Delete(String elementText, String chElemData) {
        String replace = elementText.replace(chElemData, "");
        return replace;
    }
}
